#!/bin/bash
USER="backup"
PASS="Aa123321"
HOST="localhost"
BACKUPDIR="/opt/backup/binary/"
MYBINLOG="/usr/local/mysql/bin/mysqlbinlog -u$USER -p$PASS"
MYCMD="/usr/local/mysql/bin/mysql -u$USER -p$PASS"
#7天前的时间
TIME=$(date "-d 7 day ago" +"%F %T")
#3小时之前
STARTTIME=$(date "-d -3hour" +"%F %T")
BACKTIME=$(date +"%F_%H")
#创建文件
[ ! -d $BACKUPDIR/${BACKTIME} ] && mkdir -p $BACKUPDIR/${BACKTIME}
#刷新,生成新的二进制文件
$MYCMD -e "flush logs"
for dbname in $($MYCMD -e "show databases;"|sed 1d|egrep -v "_schema|test");do
	for log in `cat /data/mysql-bin.index`;do 
		base=$(basename $log)
 		$MYBINLOG -d $dbname --start-datetime="$STARTTIME" /data/${base} >>${BACKUPDIR}/${BACKTIME}/${dbname}.sql
	done
done
#删除7天前的二进制
cd $BACKUPDIR && find . -type d -mtime +7 |xargs rm -f

